package jp.tf_web.httpserversample.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ProtocolVersion;
import org.apache.http.RequestLine;
import org.apache.http.StatusLine;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.message.BasicLineParser;
import org.apache.http.message.BufferedHeader;
import org.apache.http.util.CharArrayBuffer;

import android.util.Log;
import android.webkit.MimeTypeMap;

public class HttpUtil {
	public final static String HTTP_PROTOCOL = "HTTP/1.1";
	public final static String HEADER_NAME_REQUESTLINE = "RequestLine";
	public final static String HEADER_NAME_CONTENT_LENGTH = "Content-Length";
	
	private HttpUtil(){}
	
	//リクエストヘッダー　パーサー
	public static HashMap<String,Object> parseRequestHeader(String headers_str) throws IOException{
		//改行コードで分割
		StringReader sr = new StringReader(headers_str);
		BufferedReader bur = new BufferedReader(sr);

		HashMap<String,Object> headerList = new HashMap<String,Object>();
		String line;
		int line_index = 0;
		while((line = bur.readLine()) != null){
			if(line.length() == 0) break;
			
			//１行づつ取り出す
			if(line_index == 0){
				//リクエストライン
				BasicLineParser parser = new BasicLineParser();
				RequestLine request_line = BasicLineParser.parseRequestLine(line, parser);
				headerList.put(HttpUtil.HEADER_NAME_REQUESTLINE,request_line);
			}
			else{
				//ヘッダー
				CharArrayBuffer cab = new CharArrayBuffer(line.length());
				cab.append(line);
				Header h = new BufferedHeader(cab);
				headerList.put(h.getName(),h);
			}
			
			line_index++;
		}
		bur.close();
		sr.close();
		
		return headerList;
	}
	
	//ステータスラインを作成
	public static StatusLine createStatusLine(int status,String reason){
		BasicLineParser parser = new BasicLineParser();
		return BasicLineParser.parseStatusLine(HTTP_PROTOCOL+" "+status+" "+reason,parser);
	}
	
	//HTTPレスポンスを作成
	public static HttpResponse createHttpResponse(int status,String reason,HttpEntity entity){
		StatusLine header_sl = HttpUtil.createStatusLine(status,reason);
		HttpResponse hr = new BasicHttpResponse(header_sl);
		hr.setEntity(entity);		
		return hr; 
	}
	
	public static HttpEntity createHttpEntity(String contentType,byte[] entity_buf){
		ByteArrayEntity he = new ByteArrayEntity(entity_buf);
		he.setContentType(contentType);
		return he;
	}
	
	public static String getMimeTypeFromUri(String uri){
		//拡張子からMimeTypeを取得
		String ext = MimeTypeMap.getSingleton().getFileExtensionFromUrl(uri);
		String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
		
		//不明な場合 "application/octet-stream"
		//ToDo mime-type一覧を作りココで対応するmime-typeを取得するように変更してもいい
		mimeType = (mimeType != null)?mimeType:"application/octet-stream";
		Log.d(HttpUtil.class.getName(),"ext:"+ext+" mimeType:"+mimeType);

		return mimeType;
	}
}
